package com.hfzy.ihk.web.websocket.conf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

import java.net.URL;

/**
 * Created by Administrator on 2018/1/16.
 *
 * @author wws
 */
@Configuration
/*If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.*/
//@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    ApplicationContext applicationContext;

    @Bean
    @Profile({"local"})
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        URL resource = this.getClass().getClassLoader().getResource("templates/");//这里把系统获取到的Class的path替换为源码对应的Path，这样修改的时候就可以动态刷新
        String devResource = resource.getFile().toString().replaceAll("build/resources/main", "src/main/resources");
        resolver.setPrefix("file:"+devResource); //不允许缓存
        resolver.setCacheable(false);
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML");
        return resolver;
    }
    @Bean
    @Profile({"local"})
    public ITemplateResolver templateResolver1() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        URL resource = this.getClass().getClassLoader().getResource("templates/");//这里把系统获取到的Class的path替换为源码对应的Path，这样修改的时候就可以动态刷新
        String devResource = resource.getFile().toString().replaceAll("build/resources/main", "src/main/resources");
        resolver.setPrefix("file:"+devResource); //不允许缓存
        resolver.setCacheable(false);
        resolver.setSuffix(".js");
        resolver.setTemplateMode("HTML");
        return resolver;
    }
}
